package com.mapper;

import com.domain.User_data;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.service.Public_funtion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class User_mysql {

    //快递柜当前柜子存放信息
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Resource
    private Public_funtion p;




    public List<User_data> get_user(User_data data)
    {

        String sql="select * FROM User WHERE"+
                " id  LIKE '%' "+
                p.sql_null_selsect("openid",data.getOpenid())+
                p.sql_null_selsect("userid",data.getUserid())+
                p.sql_null_selsect("account",data.getAccount())+
                p.sql_null_selsect("password",data.getPassword())+
                p.sql_null_selsect("nickname",data.getNickname())+
                p.sql_null_selsect("username",data.getUsername())+
                p.sql_null_selsect("phone",data.getPhone())+
                p.sql_null_selsect("id_card",data.getId_card())+
                p.sql_null_selsect("score",String.valueOf(data.getScore()))+
                p.sql_null_selsect("member",String.valueOf(data.getMember()))+
                p.sql_null_selsect("addr",data.getAddr())+
                p.sql_null_selsect("sex",String.valueOf(data.getSex()))+
                p.sql_null_selsect("login_time",data.getLogin_time())+
                p.sql_null_selsect("type",data.getType())+
                p.sql_null_selsect("subscribe_time",data.getSubscribe_time())+
                p.sql_null_selsect("remark",data.getRemark());



        List list = jdbcTemplate.queryForList(sql);
        if(list.size()>0)
        {
            return LLLS_to_Class(list) ;
        }
        else
        {
            return list;
        }
    }


    public List<Map<String, Object>> get_box_over_time(User_data data)
    {

        String sql="select * FROM Box WHERE  time < '"+data.getSubscribe_time()+"'";
        List list = jdbcTemplate.queryForList(sql);
        if(list.size()>0)
        {
            return list;
        }
        else
        {
            p.print_log("人员列表中没有数据",5);
            return list;
        }
    }


    public String insert_user_data(User_data data)
    {
        String send_string = "";
        String sql="INSERT INTO User (id,openid,userid,account,password,nickname,username,phone,id_card,score,member,addr,sex,login_time,type,subscribe_time,remark)" +

                " VALUES(" +
                "'',"+
                "'"+data.getOpenid()+"',"+
                "'"+data.getUserid()+"',"+
                "'"+data.getAccount()+"',"+
                "'"+data.getPassword()+"',"+
                "'"+data.getNickname()+"',"+
                "'"+data.getUsername()+"',"+
                "'"+data.getPhone()+"',"+
                "'"+data.getId_card()+"',"+
                "'"+data.getScore()+"',"+
                "'"+data.getMember()+"',"+
                "'"+data.getAddr()+"',"+
                "'"+data.getSex()+"',"+
                "'"+data.getLogin_time()+"',"+
                "'"+data.getType()+"',"+
                "'"+data.getSubscribe_time()+"',"+
                "'"+data.getRemark()+"'"+
                ")";

        sql=sql.replaceAll("-1","");
        sql=sql.replaceAll("null","");
        sql=sql.replaceAll("''","NULL");
       jdbcTemplate.update(sql);
        p.print_log( "快递柜物品已经放入快递柜"+data.getPhone(),7);
        return "write_ok";
    }

    public String updata_box_type(User_data data,String type, String value)
    {
        String send_string = "";
        String sql="UPDATE User set " +
                  "openid='"+data.getOpenid()
                +"', userid='"+data.getUserid()
                +"', account='"+data.getAccount()
                +"', password='"+data.getPassword()
                +"', nickname='"+data.getNickname()
                +"', username='"+data.getUsername()
                +"', phone='"+data.getPhone()
                +"', id_card='"+data.getId_card()
                +"', score='"+data.getScore()
                +"', member='"+data.getMember()
                +"', addr='"+data.getAddr()
                +"', sex='"+data.getSex()
                +"', login_time='"+data.getLogin_time()
                +"', type='"+data.getType()
                +"', subscribe_time='"+data.getSubscribe_time()
                +"', remark='"+data.getRemark()
                +"' WHERE  '"+type+"' like '"+value+"'";
        List list = jdbcTemplate.queryForList(sql);
        return "黑名单人员姓名已经添加";
    }

    public String remove_user_type(String type, String value )
    {
        String send_string = "";
        String sql="DELETE FROM User  WHERE    '"+type+"'= '"+value+"'";
        List list = jdbcTemplate.queryForList(sql);
        if(list.size()>0)
        {
            Object MSO = list.get(0);
            return "ok";
        }
        else
        {
            System.out.println("黑名单name名字错误，未找到该姓名，请重新设置");
            return "err";
        }
    }
    public   List<User_data>  LLLS_to_Class(List list)

    {
        List<User_data> box_List= new ArrayList();
        User_data mysql_list = new User_data();
        for (int i=0 ; i<list.size();i++)
        {
            Object MSO = list.get(i);
            ObjectMapper objectMapper = new ObjectMapper();
            mysql_list = objectMapper.convertValue(MSO, User_data.class);
            box_List.add(mysql_list);
        }
        return box_List;
    }
}
